package com.adhere.auth.controller;

import com.adhere.auth.pojo.LoginBody;
import com.adhere.auth.pojo.LoginUser;
import com.adhere.auth.service.LoginService;
import com.adhere.auth.service.TokenService;
import com.adhere.common.pojo.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 登录相关接口
 *
 * @author WT
 * @date 2022/02/07 14:28:11
 */
@RestController
public class LoginController {

    private final LoginService loginService;
    private final TokenService tokenService;

    public LoginController (LoginService loginService, TokenService tokenService) {
        this.loginService = loginService;
        this.tokenService = tokenService;
    }

    /**
     * 用户登录返回 accessToken
     * @param loginBody 用户登录信息
     * @return accessToken
     */
    @PostMapping("/login")
    public Result<Map<String, String>> login(@RequestBody @Validated LoginBody loginBody) {

        LoginUser loginUser = loginService.login(loginBody);

        // 获取登录token
        return Result.success(tokenService.createToken(loginUser));

    }

}
